package com.dk.controller;

import com.dk.pojo.Rights;
import com.dk.pojo.Talent;
import com.dk.service.RightsService;
import com.dk.service.TalentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("talent")
public class TalentController {
    @Autowired
    private TalentService talentService;
    @Autowired
    private RightsService rightsService;

    @RequestMapping("toShow")
    public String toShow(){
        return "/talent/show";
    }


    @RequestMapping("getInfo")
    @ResponseBody
    public Map<String,Object> getInfoServer(@RequestParam(defaultValue = "1")Integer pageNumber,
                                            @RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<Talent> page =  talentService.show(pageNumber,pageSize);
        //需要total:总条数 rows:结果集 (都是key,value结合,所以用map)
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getList());
        return map;

    }

    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo(Talent talent){
        return talentService.addInfo(talent);
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<Rights> rights = rightsService.getInfo();
        model.addAttribute("rights",rights);
        return "/talent/add";
    }

    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
        Talent talent = talentService.toUpdatet(id);
        model.addAttribute("talent",talent);
        List<Rights> rights = rightsService.getInfo();
        model.addAttribute("rights",rights);
        return "/talent/update";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(Talent talent){
        return talentService.updateInfo(talent);
    }

    @RequestMapping("deleteInfo/{id}")
    @ResponseBody
    public boolean deleteInfo(@PathVariable("id")Integer id){
        return talentService.deleteInfo(id);
    }



}
